package cn.com.ghy.android.widget.salemag;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import cn.com.ghy.android.bean.YGQueryInfoBean;
import cn.com.ghy.android.exception.DisposeException;
import cn.com.ghy.android.service.YgOrderOpeService;
import cn.com.ghy.android.style.CustomProgressDialog;
import cn.com.ghy.android.util.CommonStatic;
import cn.com.ghy.android.widget.ComStaticApp;
import cn.com.ghy.android.widget.R;

public class SaleOrderBaseInfoQuery extends Activity {

	private View appToolBar;
	private ImageButton btnList;
	private ImageButton btnBack;

	private EditText txtNo;
	private EditText txtDescribe;
	private Button btnQuery;
	private ListView listSaleOrderBaseInfoLay;
	private ProgressDialog progressDialog;

	private Intent intent;

	private List<YGQueryInfoBean> yGQueryInfoBeanList;
	private int btnRId;
	private String key;
	private String value;
	private YGOrderBaseInfoListAdapter yGOrderBaseInfoListAdapter;
	private Map<String, String> mapValue = new HashMap<String, String>();

	private DisposeException disposeException;
	private Exception error;

	private HttpThread httpThread;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sale_order_baseinfo_query);
		setToolBar();
		getSession();
		findView();
		init();
		addListener();
	}

	@SuppressWarnings("unchecked")
	public void getSession() {
		disposeException = ((ComStaticApp) getApplication()).getDisposeException();
		intent = this.getIntent();
		yGQueryInfoBeanList = (List<YGQueryInfoBean>) intent.getSerializableExtra("queryInfoList");
		btnRId = intent.getIntExtra("btnRId", 0);
		key = intent.getStringExtra("key");
		value = intent.getStringExtra("value");
		mapValue = (Map<String, String>) intent.getSerializableExtra("mapValue");
	}

	private void findView() {
		txtNo = (EditText) findViewById(R.id.txtNo);
		txtDescribe = (EditText) findViewById(R.id.txtDescribe);
		btnQuery = (Button) findViewById(R.id.btnQuery);

		listSaleOrderBaseInfoLay = (ListView) findViewById(R.id.listSaleOrderBaseInfoLay);
	}

	private void setToolBar() {
		appToolBar = findViewById(R.id.appToolBar);
		btnList = (ImageButton) appToolBar.findViewById(R.id.btnList);
		btnList.setVisibility(View.GONE);
		btnBack = (ImageButton) appToolBar.findViewById(R.id.btnBack);
		btnBack.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				SaleOrderBaseInfoQuery.this.finish();
			}
		});
	}

	private void init() {
		txtNo.setText(value);
		yGOrderBaseInfoListAdapter = new YGOrderBaseInfoListAdapter(SaleOrderBaseInfoQuery.this,
				yGQueryInfoBeanList);
		listSaleOrderBaseInfoLay.setAdapter(yGOrderBaseInfoListAdapter);
		listSaleOrderBaseInfoLay.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				YGQueryInfoBean bean = (YGQueryInfoBean) parent.getAdapter().getItem(position);
				showDialog(SaleOrderBaseInfoQuery.this, bean);
			}
		});
	}

	private void addListener() {
		btnQuery.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				operate(v.getId());
			}
		});
	}

	private void operate(int opcode) {
		progressDialog = new CustomProgressDialog(SaleOrderBaseInfoQuery.this);
		progressDialog.setButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				httpThread.stopSendMsg();
			}
		});
		progressDialog.show();

		httpThread = new HttpThread(opcode);
		Thread thread = new Thread(httpThread);
		thread.start();
	}

	private class HttpThread implements Runnable {

		private int opcode;

		public HttpThread(int opcode) {
			this.opcode = opcode;
		}

		private boolean isSendMsg = true;

		@Override
		public void run() {
			Message message = handler.obtainMessage();
			YgOrderOpeService service = new YgOrderOpeService();
			try {
				Map<String, String> info = new HashMap<String, String>();
				info.put("SEARCH_VAL", txtNo.getText().toString());
				info.put("SEARCH_TITLE", txtDescribe.getText().toString());
				mapValue.remove(key);
				Iterator<Entry<String, String>> it = mapValue.entrySet().iterator();
				while (it.hasNext()) {
					String key = it.next().getKey();
					info.put(key, mapValue.get(key));
				}
				yGQueryInfoBeanList = service.GetSlt(info, key);
			} catch (Exception e) {
				opcode = CommonStatic.ERROR_CODE;
				error = e;
			}
			if (isSendMsg) {
				message.what = opcode;
				handler.sendMessage(message);
			}
		}

		public void stopSendMsg() {
			isSendMsg = false;
		}

		@SuppressWarnings("unused")
		public void openSendMsg() {
			isSendMsg = true;
		}
	}

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			progressDialog.dismiss();
			switch (msg.what) {
				case R.id.btnQuery :
					yGOrderBaseInfoListAdapter = new YGOrderBaseInfoListAdapter(
							SaleOrderBaseInfoQuery.this, yGQueryInfoBeanList);
					listSaleOrderBaseInfoLay.setAdapter(yGOrderBaseInfoListAdapter);
					break;
				case CommonStatic.ERROR_CODE :
					disposeException.handleException(error);
					break;
				default :
					break;
			}
		}
	};

	private void showDialog(final Context context, final YGQueryInfoBean bean) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("确认选择");
		builder.setMessage("编号：" + bean.getVal() + "\n" + "描述：" + bean.getTitle());
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				intent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putInt("btnRId", btnRId);
				bundle.putString("key", key);
				bundle.putString("value", bean.getVal());
				intent.putExtras(bundle);
				SaleOrderBaseInfoQuery.this.setResult(RESULT_OK, intent);
				SaleOrderBaseInfoQuery.this.finish();
			}
		});
		builder.setNegativeButton("取消", null);
		builder.show();
	}

	private class YGOrderBaseInfoListAdapter extends BaseAdapter {

		private Context context = null;
		private List<YGQueryInfoBean> list = new ArrayList<YGQueryInfoBean>();

		public YGOrderBaseInfoListAdapter(Context context, List<YGQueryInfoBean> list) {
			this.context = context;
			if (list != null) {
				this.list = list;
			}
		}

		public int getCount() {
			return list.size();
		}

		public YGQueryInfoBean getItem(int position) {
			return list.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			String inflater = Context.LAYOUT_INFLATER_SERVICE;
			LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(inflater);
			LinearLayout linearLayout = null;
			YGQueryInfoBean bean = list.get(position);
			linearLayout = (LinearLayout) layoutInflater.inflate(
					R.layout.sale_order_baseinfo_query_lay, null);
			TextView textView = ((TextView) linearLayout.findViewById(R.id.labNo));
			textView.setText(bean.getVal());

			textView = ((TextView) linearLayout.findViewById(R.id.labDescribe));
			textView.setText(bean.getTitle());

			return linearLayout;
		}

	}
}
